package cn.likun.kit.file.bean;

import java.util.Comparator;
import java.util.Date;

public class FileOrderBean implements Comparator<FileBean>{

	public enum OderModeEnum {
		NAME,
		TYPE,
		SIZE,
		LAST_MODIFIED
	}
	
	private OderModeEnum oderMode = OderModeEnum.NAME;
	
	private Boolean isDesc = false;
	
	public OderModeEnum getOderMode() {
		return oderMode;
	}
	public void setOderMode(OderModeEnum oderMode) {
		this.oderMode = oderMode;
	}
	public Boolean getIsDesc() {
		return isDesc;
	}
	public void setIsDesc(Boolean isDesc) {
		this.isDesc = isDesc;
	}
	
	public FileOrderBean() {}
	
	public FileOrderBean(OderModeEnum oderMode, Boolean isDesc) {
		this.oderMode = oderMode;
		this.isDesc = isDesc;
	}
	/**
	 * 根据第一个参数小于、等于或大于第二个参数分别返回负整数、零或正整数。 
	 */
	@Override
	public int compare(FileBean fileBean1, FileBean fileBean2) {
		if (isDesc) {
			return compareStraight(fileBean2, fileBean1);
		}
		return compareStraight(fileBean1, fileBean2);
	}
	protected int compareStraight(FileBean fileBean1, FileBean fileBean2) {
		Boolean isFolder1 = fileBean1.getIsFolder();
		Boolean isFolder2 = fileBean2.getIsFolder();
		if (isFolder1 && isFolder2) {
			/**文件夹之间的比较*/
			return order(fileBean1, fileBean2);
		} else if (isFolder1 && !isFolder2) {
			/**文件夹小于文件*/
			return -1;
		} else if (!isFolder1 && isFolder2) {
			/**文件大于文件夹*/
			return 1;
		} else {
			/**文件之间的比较*/
			return order(fileBean1, fileBean2);
		}
	}
	
	protected int order(FileBean fileBean1, FileBean fileBean2) {
		if (oderMode.equals(OderModeEnum.NAME)) {
			String name1 = fileBean1.getName();
			String name2 = fileBean2.getName();
			return name1.compareTo(name2);
		} else if (oderMode.equals(OderModeEnum.SIZE)) {
			Long size1 = fileBean1.getSize();
			Long size2 = fileBean2.getSize();
			return size1.compareTo(size2);
		} else if (oderMode.equals(OderModeEnum.TYPE)) {
			String type1 = fileBean1.getType();
			String type2 = fileBean2.getType();
			return type1.compareTo(type2);
		} else if (oderMode.equals(OderModeEnum.LAST_MODIFIED)) {
			Date date1 = fileBean1.getLastModified();
			Date date2 = fileBean2.getLastModified();
			return date1.compareTo(date2);
		}
		return 0 ;
	}
}




























